package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.juneng.common.Condition;
import com.juneng.dto.SmsBannerBookDto;
import com.juneng.entity.BookDiscount;
import com.juneng.entity.CourseList;
import com.juneng.entity.SmsBannerBook;
import com.juneng.mapper.BookDiscountMapper;
import com.juneng.mapper.BookTypeMapper;
import com.juneng.mapper.SmsBannerBookMapper;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.BookMsg;
import com.juneng.mapper.BookMsgMapper;
import com.juneng.service.IBookMsgService;
import com.juneng.dto.BookMsgDto;
import com.juneng.dto.BookMsgQueryDto;
import com.juneng.vo.BookMsgVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author zds
 * @since 2021-07-13
 */
@Service
@AllArgsConstructor
public class BookMsgServiceImpl implements IBookMsgService {


    protected BookMsgMapper bookMsgMapper;

    protected BookTypeMapper bookTypeMapper;

    private SmsBannerBookMapper smsBannerBookMapper;

    private BookDiscountMapper bookDiscountMapper;

    @Override
    public IPage<BookMsgVo> page(BookMsgQueryDto dto) {
        IPage<BookMsgVo> page = Condition.getPage(dto);
        IPage<BookMsgVo> page1 = bookMsgMapper.page(page, dto);
        List<BookMsgVo> records = page1.getRecords();
        for (BookMsgVo record : records) {
            List<SmsBannerBook> smsBannerBooks = smsBannerBookMapper.selectList(new LambdaQueryWrapper<SmsBannerBook>().eq(SmsBannerBook::getBookId, record.getBookId()));
            List<String> collect = smsBannerBooks.stream().map(SmsBannerBook::getBannerPicture).collect(Collectors.toList());
            record.setBannerPicture(collect);
        }
        return page1;
    }
    //discount  sellWell
    @Override
    public List<BookMsgVo> list(BookMsgQueryDto dto) {
        List<BookMsgVo> list = new ArrayList<>();
        QueryWrapper<BookMsg> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto,BookMsg.class));
        List<BookMsg> bookMsgs = bookMsgMapper.selectList(queryWrapper);
        for (BookMsg bookMsg : bookMsgs) {
            List<SmsBannerBook> smsBannerBooks = smsBannerBookMapper.selectList(new LambdaQueryWrapper<SmsBannerBook>().eq(SmsBannerBook::getBookId, bookMsg.getBookId()));
            List<String> collect = smsBannerBooks.stream().map(SmsBannerBook::getBannerPicture).collect(Collectors.toList());
            BookMsgVo copy = BeanCopyUtils.copy(bookMsg, BookMsgVo.class);
            copy.setBannerPicture(collect);
            list.add(copy);
        }
        return list;
    }

    @Override
    public Integer save(BookMsgDto dto) {
        if (dto.getDiscount() == 1 && dto.getSellWell() == 1 ){
            dto.setBookSpeciesId("1,2");
        }else if (dto.getDiscount() ==1){
            dto.setBookSpeciesId("2");
        }else if (dto.getSellWell() == 1){
            dto.setBookSpeciesId("1");
        }

        return bookMsgMapper.insert(BeanCopyUtils.copy(dto,BookMsg.class));
    }

    @Override
    public Integer updateById(BookMsgDto dto) {
         //修改轮播图   sell_well_position
        BookMsg bookMsg = BeanCopyUtils.copy(dto, BookMsg.class);
        if (bookMsg.getSellWellPosition() !=0 && dto.getDiscount() != 0) {       //畅销榜显示第几本
            List<BookMsg> bookMsg2 = bookMsgMapper.selectList(new LambdaQueryWrapper<BookMsg>().
                    eq(BookMsg::getBookTypeId, bookMsg.getBookTypeId()).
                    eq(BookMsg::getSellWellPosition, bookMsg.getSellWellPosition()));
            if (bookMsg2.size() != 0) {
                for (BookMsg msg : bookMsg2) {
                    BookMsg bookMsg4 = new BookMsg();
                    bookMsg4.setBookId(msg.getBookId());
                    bookMsg4.setSellWellPosition(0);
                    bookMsgMapper.updateById(bookMsg4);
                }
            }
        }
        if (bookMsg.getDisplayPosition() != 0 && dto.getSellWell() != 0 ){       //折扣区显示第几本
            List<BookMsg> bookMsg1 = bookMsgMapper.selectList(new LambdaQueryWrapper<BookMsg>()
                    .eq(BookMsg::getDisplayPosition, bookMsg.getDisplayPosition()).eq(BookMsg::getBookDiscountId, bookMsg.getBookDiscountId()));
            if (bookMsg1.size() != 0){
                for (BookMsg msg : bookMsg1) {
                    BookMsg bookMsg2 = new BookMsg();
                    bookMsg2.setBookId(msg.getBookId());
                    bookMsg2.setDisplayPosition(0);
                    bookMsgMapper.updateById(bookMsg2);
                }
            }
        }
        if (dto.getDiscount() == 1 && dto.getSellWell() == 1 ){
            bookMsg.setBookSpeciesId("1,2");
        }else if (dto.getDiscount() ==1){
            bookMsg.setBookSpeciesId("2");
        }else if (dto.getSellWell() == 1){
            bookMsg.setBookSpeciesId("1");
        }
        return  bookMsgMapper.updateById(bookMsg);
    }

    @Override
    public Integer deleteLogic(Long id) {
        return bookMsgMapper.deleteById(id);
    }

    @Override
    public BookMsgVo getById(Long id) {
        BookMsgVo bookMsgVo = bookMsgMapper.getById(id);
        if (bookMsgVo.getBookSpeciesId() != null &&"2".contains(bookMsgVo.getBookSpeciesId())){
            bookMsgVo.setDiscount(1);
            BookDiscount bookDiscount = bookDiscountMapper.selectOne(new LambdaQueryWrapper<BookDiscount>().eq(BookDiscount::getBookDiscountId, bookMsgVo.getBookDiscountId()));
            if (bookDiscount != null){
                bookMsgVo.setBookAFewFold(bookDiscount.getBookAFewFold());
            }

        }else if (bookMsgVo.getBookSpeciesId() != null && "1".contains(bookMsgVo.getBookSpeciesId())){
            bookMsgVo.setSellWell(1);
        }

        return bookMsgVo;
    }

    @Override
    public HashMap<String, Object> bannerBookList(BookMsgQueryDto dto) {
        List<SmsBannerBook> smsBannerBooks = smsBannerBookMapper.selectList(new LambdaQueryWrapper<SmsBannerBook>().eq(SmsBannerBook::getBookId, dto.getBookId()));
        List<String> collect = smsBannerBooks.stream().map(SmsBannerBook::getBannerPicture).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("bannerUrl",  collect);
        return map;
    }

    /**
     * 添加轮播图 discount sellWell
     * @param dto
     * @return
     */
    @Override
    public Integer bannerBookSave(SmsBannerBookDto dto) {
        List<SmsBannerBook> smsBannerBooks = smsBannerBookMapper.selectList(new LambdaQueryWrapper<SmsBannerBook>().eq(SmsBannerBook::getBookId, dto.getBookId()));
        if (smsBannerBooks.size() != 0){

        }
        return smsBannerBookMapper.insert(BeanCopyUtils.copy(dto, SmsBannerBook.class));
    }
}
